package com.dream.service.recommender.web.processor.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.common.toolkits.aws.CloudWatchClient;
import com.dream.common.toolkits.aws.NameSpace;
import com.dream.service.recommender.api.RouteService;
import com.dream.service.recommender.domain.request.BaseRequest;
import com.dream.service.recommender.domain.request.FrontParam;
import com.dream.service.recommender.domain.response.RecommenderData;
import com.dream.service.recommender.web.parse.Parser;
import com.dream.service.recommender.web.parse.impl.ParamParser;
import com.dream.service.recommender.web.processor.RequestProcessor;

import javax.servlet.http.HttpServletRequest;
import java.security.InvalidParameterException;

/**
 * Created by zhiji on 2016/3/24.
 */
public class RouteRequestProcessor implements RequestProcessor {

    private RouteService routeService;

    public RouteService getRouteService() {
        return routeService;
    }

    public void setRouteService(RouteService routeService) {
        this.routeService = routeService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {

        long sTime = System.currentTimeMillis();
        JSONObject result = new JSONObject();
        if (routeService == null) {
            AppResultBuilder.buildProcessErrorResult(result, "");
            return result;
        }
        String recid= req.getParameter("recid");
        if("999999".equals(recid)){
            JSONArray jsonArray = new JSONArray();
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("name","newest asc");
//            jsonObject.put("id",1);
//            jsonArray.add(jsonObject);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name","Newest  Arrivals");
            jsonObject1.put("id",2);
            jsonArray.add(jsonObject1);

            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("name","Low to High");
            jsonObject2.put("id",3);
            jsonArray.add(jsonObject2);

            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("name","High to Low");
            jsonObject3.put("id",4);
            jsonArray.add(jsonObject3);

//            JSONObject jsonObject4 = new JSONObject();
//            jsonObject4.put("name","sales asc");
//            jsonObject4.put("id",5);
//            jsonArray.add(jsonObject4);

            JSONObject jsonObject5 = new JSONObject();
            jsonObject5.put("name","Best Selling");
            jsonObject5.put("id",6);
            jsonArray.add(jsonObject5);

            AppResultBuilder.buildSucResult(result, jsonArray);
            return result;
        }
        try {
            Parser parser = new ParamParser();
            FrontParam frontParam = null;
            try {
                frontParam = parser.parse(req);
            } catch (InvalidParameterException e) {
                AppResultBuilder.buildParamErrorResult(result, e.getMessage());
                return result;
            }
            BaseRequest<FrontParam> baseRequest = new BaseRequest<FrontParam>();
            baseRequest.setParam(frontParam);
            BaseResponse<RecommenderData> baseResponse = routeService.getRecommender(baseRequest);
            if (baseResponse.isSuccess() && baseResponse.getError_code() == ErrorCode.OK.getCode()) {
                RecommenderData recommenderData = baseResponse.getResult();
                if (recommenderData != null && recommenderData.getList() != null && !recommenderData.getList().isEmpty()) {
                    AppResultBuilder.buildSucResult(result, recommenderData);
                } else {
                    AppResultBuilder.buildSucResult(result, new JSONObject());

                }
//            rec_100000=categoryListRecommender
//            rec_100001=dailyFillListRecommender
//            rec_100002=productDetailRecommender
//            rec_100003=categoryListPDRecommender
//            rec_100004=designerDetailRecommender
//            rec_100005=footStepRecommender
//            #rec_200000=ppcPosRecommender
//            #rec_200001=ppcListRecommender
//            rec_200000=ppcRandEnterRecommender
//            rec_200001=ppcCrossRecommender
//            rec_200002=ppcRandEnterRecommender
//                System.out.println(frontParam.getRec_id());
                long response_time = System.currentTimeMillis() - sTime;

//                CloudWatchClient.sendResponseTime(NameSpace.EM_REC, "rec_".concat(frontParam.getRec_id()), response_time);
                return result;
            }


            AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), "");
            return result;
        } catch (Throwable e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "");
            return result;
        }
    }
}
